<?php

/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author xuanyan <xuanyan@geek-zoo.com>
 *
 */

define('ROOT_PATH', dirname(__FILE__));
define('SITE_URL', 'http://'.$_SERVER['HTTP_HOST']);

require_once ROOT_PATH.'/Library/__init__.php';
require_once ROOT_PATH.'/config.php';

// Cookie::setDomain('.xxxx.com');
Database::$instance = Database::connect(Config::get('db'));
Database::$instance->initialization = array(
    'SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary',
    "SET sql_mode=''"
);

Storage::config(array(
    'db' => Database::$instance,
    'index_table' => 'admin_storage_index',
    'mate_table' => 'admin_storage_mate',
    'data_table' => 'admin_storage_data',
));

Model::$dir = ROOT_PATH.'/Models';

Smarty3::instance()->setTemplateDir(ROOT_PATH.'/Templates');
Smarty3::instance()->compile_dir = ROOT_PATH.'/tmp/compile/';

// require_once ROOT_PATH.'/Helper/Smarty.php';
// require_once ROOT_PATH.'/Helper/common.php';
// require_once ROOT_PATH.'/Helper/abstract.php';

try {
    $result = Controller::dispatch(@$_SERVER['REDIRECT_URL'], ROOT_PATH.'/Controllers');
} catch (Exception $e) {
    // out put 404 page
    if ($e->getCode() == 404) {
        header("HTTP/1.0 404 Not Found");
        require ROOT_PATH.'/404.html';
        exit;
    }
    die($e);
}

if ($result && Controller::$format == 'json') {
    echo json_encode($result);
}

?>